//
//  hQnymxAppDelegate.swift
//  QRScan
//
//  Created by Qiang on 2018/3/25.
//  Copyright © 2018年 Qiang. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate,UNUserNotificationCenterDelegate {

    let jPushAppKey = "4b12f597412b347f1f593441"
    
    var window: UIWindow?
    
    var baseTabBar : gKhZJgJCTabbarController?

    

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        UIApplication.shared.statusBarStyle = UIStatusBarStyle.lightContent
//        window?.rootViewController = baseTabBar
//        window?.makeKeyAndVisible()
//        self.tuisong(launchOptions: launchOptions,jush: jPushAppKey)
//        let  time = NSDate().timeIntervalSince1970
//        print("\(time)")
        return true
    }

    // MARK:推送
    
    func tuisong(launchOptions:[UIApplicationLaunchOptionsKey: Any]?,jush:String) {
        //省略其它代码......
        
//        //极光推送部署
//        let entity = JPUSHRegisterEntity()
//        entity.types = Int(JPAuthorizationOptions.alert.rawValue | JPAuthorizationOptions.sound.rawValue | JPAuthorizationOptions.badge.rawValue)
//        //        jush  432a80df8d9ebe04a1f377f0
//        JPUSHService.register(forRemoteNotificationConfig: entity, delegate: self)
//        JPUSHService.setup(withOption: launchOptions, appKey: jush, channel: "Publish channel", apsForProduction: false)
//
//        JPUSHService.registrationIDCompletionHandler { (resCode, registrationID) in
//            if resCode == 0{
//                print("registrationID获取成功：\(String(describing: registrationID))")
//            }else {
//                print("registrationID获取失败：\(String(describing: registrationID))")
//            }
//        }
    }
    
    
    
    func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
        application.applicationIconBadgeNumber = 0
        application.cancelAllLocalNotifications()
    }
    
    
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        
//        JPUSHService.registerDeviceToken(deviceToken)
    }
    
    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
        print("did Fail To Register For Remote Notifications With Error:\(error)")
    }
    /**
     收到静默推送的回调
     
     @param application  UIApplication 实例
     @param userInfo 推送时指定的参数
     @param completionHandler 完成回调
     */
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        
//        JPUSHService.handleRemoteNotification(userInfo)
        print("iOS7及以上系统，收到通知:\(userInfo)")
        completionHandler(UIBackgroundFetchResult.newData)
    }
    
    func application(_ application: UIApplication, didReceive notification: UILocalNotification) {
//        JPUSHService.showLocalNotification(atFront: notification, identifierKey: nil)
    }
    
    @available(iOS 10.0, *)
    func jpushNotificationCenter(_ center: UNUserNotificationCenter!, willPresent notification: UNNotification!, withCompletionHandler completionHandler: ((Int) -> Void)!) {
        
        let userInfo = notification.request.content.userInfo
        
        //        let request = notification.request; // 收到推送的请求
        //        let content = request.content; // 收到推送的消息内容
        //
        //        let badge = content.badge;  // 推送消息的角标
        //        let body = content.body;    // 推送消息体
        //        let sound = content.sound;  // 推送消息的声音
        //        let subtitle = content.subtitle;  // 推送消息的副标题
        //        let title = content.title;  // 推送消息的标题
        let dic = NSMutableDictionary(capacity: 0)
        
        if (notification.request.trigger?.isKind(of: UNPushNotificationTrigger.self))!{
            print("iOS10 前台收到远程通知:\(userInfo)")
//            JPUSHService.handleRemoteNotification(userInfo)
            
        }else {
            // 判断为本地通知
            print("iOS10 前台收到本地通知:\(userInfo)")
        }
        dic.setDictionary(userInfo)
        if dic.count != 0 {
            UserDefaults.standard.set(dic, forKey: "msg")
        }
        
        completionHandler(Int(UNAuthorizationOptions.alert.rawValue | UNAuthorizationOptions.sound.rawValue | UNAuthorizationOptions.badge.rawValue))// 需要执行这个方法，选择是否提醒用户，有badge、sound、alert三种类型可以选择设置
    }
    
    @available(iOS 10.0, *)
    func jpushNotificationCenter(_ center: UNUserNotificationCenter!, didReceive response: UNNotificationResponse!, withCompletionHandler completionHandler: (() -> Void)!) {
        let userInfo = response.notification.request.content.userInfo
        
        if (response.notification.request.trigger?.isKind(of: UNPushNotificationTrigger.self))!{
            print("iOS10 收到远程通知:\(userInfo)")
//            JPUSHService.handleRemoteNotification(userInfo)
        }
        completionHandler()
    }


}

